package com.mall4j.cloud.product.service.impl;


import com.mall4j.cloud.api.product.constant.CategoryLevel;
import com.mall4j.cloud.product.dto.CategoryDTO;
import com.mall4j.cloud.product.mapper.AttrCategoryMapper;
import com.mall4j.cloud.product.mapper.CategoryBrandMapper;
import com.mall4j.cloud.product.mapper.CategoryMapper;
import com.mall4j.cloud.product.model.Category;
import com.mall4j.cloud.product.model.CategoryBrand;
import com.mall4j.cloud.product.service.CategoryService;
import com.mall4j.cloud.api.product.vo.CategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类
 *
 * @author JCM
 * @date 2023-04-22 13:25:00
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public List<CategoryVO> list(CategoryDTO categoryDTO) {
        return categoryMapper.list(categoryDTO);
    }

    @Override
    public CategoryVO getByCategoryId(Long categoryId) {
        return categoryMapper.getByCategoryId(categoryId);
    }

    @Override
    public List<CategoryVO> getByParentId(Long parentId) {
        return categoryMapper.getByParentId(parentId);
    }

    @Override
    public void save(Category category) {
        //如果不是根父类
        if(category.getParentId() != 0){
            CategoryVO parentCategory = getByCategoryId(category.getParentId());
            category.setPath(parentCategory.getPath()+"-"+category.getParentId());
        }else {
            category.setPath(category.getParentId()+"");
        }
        categoryMapper.save(category);
    }

    @Override
    public void update(Category category) {
        categoryMapper.update(category);
    }


    @Override
    public void deleteById(Long categoryId, Long shopId) {
        categoryMapper.deleteById(categoryId,shopId);
    }

    @Override
    public List<CategoryVO> listByOneLevel() {
        CategoryDTO categoryDTO = new CategoryDTO();
        categoryDTO.setLevel(CategoryLevel.First.value());
        List<CategoryVO> list = list(categoryDTO);
        return list;
    }

    @Override
    public List<Long> listCategoryId(Long categoryId) {
        List<Long> categoryIds = new ArrayList<>();
        List<CategoryVO> categoryVOS = getByParentId(categoryId);
        for (CategoryVO categoryVO : categoryVOS){
            categoryIds.add(categoryVO.getCategoryId());
        }
        return categoryIds;
    }


}
